package com.customization.unionpay.market.job;

import com.alibaba.fastjson.JSONObject;
import com.customization.unionpay.market.util.PropUtils;
import weaver.integration.logging.Logger;
import weaver.integration.logging.LoggerFactory;
import weaver.interfaces.schedule.BaseCronJob;

import java.util.List;

/**
 * @author sjm
 * @description 会计引擎调用浪潮相关回写（日报表、冲销、结转）
 */
public class SyncAccountingEngineCorrelationJob extends BaseCronJob {
    private static final Logger log = LoggerFactory.getLogger(SyncAccountingEngineCorrelationJob.class);

    @Override
    public void execute() {

        // 未核销预付款结转台账明细台账表单id
        int prepaymentsDetailsFormId = Integer.parseInt(com.customization.unionpay.market.util.PropUtils.BB_LOG.getPropValue("marketSubject", "carryover_of_unverified_prepayments_details_form_id"));
        String prepaymentsDetailsTableName = com.customization.unionpay.market.util.FormDataUtils.getTableName(prepaymentsDetailsFormId);
        log.info("未核销预付款结转明细台账："+prepaymentsDetailsTableName);

        // 营销活动日报表台账表单id
        int marketingActivityDailyReportFormId = Integer.parseInt(PropUtils.BB_LOG.getPropValue("marketSubject", "marketing_activity_daily_report_form_id"));
        String dailyReportTableName = com.customization.unionpay.market.util.FormDataUtils.getTableName(marketingActivityDailyReportFormId);
        log.info("dailyReportTableName："+dailyReportTableName);

        // 日报表           凭证号     pzh
        try {
            String rzzValue = com.customization.unionpay.market.util.FormDataUtils.getItemValue(marketingActivityDailyReportFormId, "zt", "入账中");
            log.info("入账中枚举值：" + rzzValue);
            List<String> rzpchList = com.customization.unionpay.market.util.DailyReportRelated.selectRzpchByZtAndPzh(rzzValue, dailyReportTableName);
            for (String rzpch : rzpchList) {
                try {
                    JSONObject pzData = com.customization.unionpay.market.util.AccountingEngineUtils.doExecuteAccountingEngine(rzpch);
                    log.info("凭证信息：" + pzData);
                    // 根据入账批次号查询日报表id
                    List<String> ids = com.customization.unionpay.market.util.DailyReportRelated.selectIdByRzpch(rzpch, dailyReportTableName);
                    log.info("ids：" + ids);
//                    for (String id : ids) {
//                        // TODO 插入凭证
//                        FormDataUtils.addDetailsData(id,dailyReportTableName,"_td1",null);
//                        // TODO 根据id修改凭证号
//                        DailyReportRelated.updatePzhById(null,id,dailyReportTableName);
//                    }
                } catch (Exception e) {
                    log.error("调用浪潮异常：" + e.getMessage());
                }
            }
        } catch (Exception e) {
            log.error("日报表定时任务异常：" + e.getMessage());
        }

        // 结转             结转凭证号 jzpzh
        try {
            List<String> pchList = com.customization.unionpay.market.util.CarryoverOfUnverifiedDetails.selectCarryoverOfUnverifiedDetailsPch(prepaymentsDetailsTableName, "jzpzh", "jzpch");
            for (String pch : pchList) {
                try {
                    JSONObject pzData = com.customization.unionpay.market.util.AccountingEngineUtils.doExecuteAccountingEngine(pch);
                    log.info("凭证信息：" + pzData);
                    List<String> ids = com.customization.unionpay.market.util.CarryoverOfUnverifiedDetails.selectIdByFieldAndPch(prepaymentsDetailsTableName, "jzpch", pch);
                    log.info("ids：" + ids);
//                    for (String id : ids) {
//                        // TODO 插入凭证
//                        FormDataUtils.addDetailsData(id,prepaymentsDetailsTableName,"_td1",null);
//                        // TODO 结转凭证号
//                        CarryoverOfUnverifiedDetails.updatePzhById(id,null,"jzpzh",prepaymentsDetailsTableName);
//                    }
                } catch (Exception e) {
                    log.error("调用浪潮异常：" + e.getMessage());
                }
            }
        } catch (Exception e) {
            log.error("冲销定时任务异常：" + e.getMessage());
        }

        // 冲销             冲销凭证号 cxpzh
        try {
            List<String> pchList = com.customization.unionpay.market.util.CarryoverOfUnverifiedDetails.selectCarryoverOfUnverifiedDetailsPch(prepaymentsDetailsTableName,"cxpzh","cxpch");
            for (String pch : pchList) {
                try {
                    JSONObject pzData = com.customization.unionpay.market.util.AccountingEngineUtils.doExecuteAccountingEngine(pch);
                    log.info("凭证信息：" + pzData);
                    List<String> ids = com.customization.unionpay.market.util.CarryoverOfUnverifiedDetails.selectIdByFieldAndPch(prepaymentsDetailsTableName, "cxpch", pch);
                    log.info("ids：" + ids);
//                    for (String id : ids) {
//                        // TODO 插入凭证
//                        FormDataUtils.addDetailsData(id,prepaymentsDetailsTableName,"_td1",null);
//                        // TODO 冲销凭证号
//                        CarryoverOfUnverifiedDetails.updatePzhById(id,null,"cxpzh",prepaymentsDetailsTableName);
//                    }
                } catch (Exception e) {
                    log.error("调用浪潮异常：" + e.getMessage());
                }
            }
        } catch (Exception e) {
            log.error("结转定时任务异常：" + e.getMessage());
        }
    }

}
